const app = getApp()
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    starCount: 0,
    forksCount: 0,
    visitTotal: 0,
    isWxLogin: false,
    userName: '请登陆',
    userId: '99999',
    userImage: '',
    UCenterBgImg: ''
  },
  attached() {
      this.getCooikeUser();
      this.getBgImg();
  },
  ready: function() {
    
  },
  methods: {
    getBgImg () {
      let _that = this;
      wx.getSystemInfo({
        success(res) {
          let _bgImg = 'http://image.bkybk.com/FrrhqPgTS05Ym29IqShQMdM3zf6H';
          _that.setData({
            UCenterBgImg: _bgImg
          });
        },
      });
    },
    getCooikeUser() {
      let _isLogin = wx.getStorageSync('isWxLogin');
      if(_isLogin){
        let _loginU = wx.getStorageSync('loginUserInfo');
        this.setData({
          userInfo: _loginU,
          isWxLogin: true
        });
      }
    },


    login(info) {
      let _that = this;
      wx.login({
        success (res) {
          if (res.code) {
            var _url = app.globalData.apiServerUrl + '/auth/wx/goLogin?code='+res.code;
            //发起网络请求
            wx.request({
              url: _url,
              data: {},
              success(rr){
                var _d = rr.data;
                if(_d.errcode){
                  wx.showToast({
                    title: '授权失败，请重新尝试',
                  });
                }else{
                  const userInfo = info.detail.userInfo;
                  userInfo.oid = _d.openid;
                  userInfo.userId = _d.userId;
                  _that.setData({
                    userInfo,
                    isWxLogin: true
                  });
                  let _upInfo = app.globalData.apiServerUrl + '/auth/wx/updateUserInfo';
                  wx.request({
                    url: _upInfo,
                    method: 'POST',
                    data: {
                      userId: userInfo.userId,
                      name: userInfo.nickName,
                      gender: userInfo.gender,
                      avatarUrl: userInfo.avatarUrl
                    },
                    header: {
                      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
                    },
                    success(ures) {
                      console.log(ures);
                    }
                  })


                  try {
                    wx.setStorageSync('loginUserId', _d.openid);
                    wx.setStorageSync('isWxLogin', true);
                    wx.setStorageSync('loginUserInfo', userInfo);
                  } catch (e) { }

                }
                

              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      });
      
    },

    logout(e) {
      try {
        wx.removeStorageSync('loginUserId');
        wx.setStorageSync('isWxLogin', false);
        wx.removeStorageSync('loginUserInfo');
        this.setData({
          userInfo: {},
          isWxLogin: false
        });
      } catch (e) {
        // Do something when catch error
      }
      
      // app.globalData.loginUserInfo = {};
      // this.setData({
      //   userInfo: null,
      //   isWxLogin: false
      // });
    },
    
    CopyLink(e) {
      wx.setClipboardData({
        data: e.currentTarget.dataset.link,
        success: res => {
          wx.showToast({
            title: '已复制',
            duration: 1000,
          })
        }
      })
    },
    showModal(e) {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    showQrcode() {
      wx.previewImage({
        urls: ['https://www.bkybk.com/static/image/zanCode.jpg'],
        current: 'https://www.bkybk.com/static/image/zanCode.jpg' // 当前显示图片的http链接      
      })
    },
  }
})